﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;
using Jy123.Web.AppCode;
using Jy123.Common;

namespace Jy123.Web.Manage
{
    public partial class GameType : MgConfig
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Verify("GameType.aspx");
            if (!Page.IsPostBack)
            {
                LoadInitialValue();
            }
        }

        protected void Repeater_DataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Jy123.Model.Public_GameType gameType = e.Item.DataItem as Jy123.Model.Public_GameType;
                HiddenField idHidden = e.Item.FindControl("idHidden") as HiddenField;
                idHidden.Value = gameType.ID.ToString();
                Label nameLabel = e.Item.FindControl("gameNameLabel") as Label;
                nameLabel.Text = gameType.TypeName;
                HtmlImage image = e.Item.FindControl("gameTypeImage") as HtmlImage;
                image.Src = gameType.Logo;
            }
        }

        protected void Repeater_Command(object sender, RepeaterCommandEventArgs e)
        {
            RepeaterItem item = e.Item as RepeaterItem;
            HiddenField hidden = e.Item.FindControl("idHidden") as HiddenField;
            int id = int.Parse(hidden.Value);
            Jy123.BLL.Public_GameType BLLGameType = new Jy123.BLL.Public_GameType();
            if (e.CommandName.Equals("Edit"))
            {
                Jy123.Model.Public_GameType gameType = BLLGameType.GetModel(id);
                actionTypeHidden.Value = "Update";
                idHidden.Value = id.ToString();
                gameNameTextBox.Text = gameType.TypeName;
            }
            else if (e.CommandName.Equals("Delete"))
            {
                BLLGameType.Delete(id);
                LoadInitialValue();
                ClearPage();
            }
        }

        protected void Save(object sender, EventArgs e)
        {
            Jy123.BLL.Public_GameType BLLGameType = new Jy123.BLL.Public_GameType();
            if (actionTypeHidden.Value.Equals("Add"))
            {
                Jy123.Model.Public_GameType gameType = new Jy123.Model.Public_GameType();
                gameType.TypeName = gameNameTextBox.Text;
                int typeId = BLLGameType.Add(gameType);
                gameType.Logo = CreateLogo(typeId);
                gameType.ID = typeId;
                BLLGameType.Update(gameType);
            }
            else
            {
                int id = int.Parse(idHidden.Value);
                Jy123.Model.Public_GameType gameType = BLLGameType.GetModel(id);
                gameType.TypeName = gameNameTextBox.Text;
                if (fileUpload.PostedFile != null)
                {
                    string strLogo = CreateLogo(id);
                    if (!string.IsNullOrEmpty(strLogo))
                    {
                        gameType.Logo = strLogo;
                    }
                }

                BLLGameType.Update(gameType);
            }
            LoadInitialValue();
            ClearPage();
        }

        private void ClearPage()
        {
            actionTypeHidden.Value = "Add";
            gameNameTextBox.Text = string.Empty;
        }

        private void LoadInitialValue()
        {
            Jy123.BLL.Public_GameType BLLGameType = new Jy123.BLL.Public_GameType();
            List<Jy123.Model.Public_GameType> listgameType = BLLGameType.GetModelList(string.Empty);
            gameTypeRepeater.DataSource = listgameType;
            gameTypeRepeater.DataBind();
            gameTypeRepeater.Visible = listgameType.Count > 0;
        }

        protected string CreateLogo(int gameTypeId)
        {
            CreateDir createDir = new CreateDir();
            HttpPostedFile postFile = fileUpload.PostedFile;
            string logoPath = postFile.FileName;
            string logoName = string.Empty;
            if (!string.IsNullOrEmpty(logoPath))
            {
                string logoType = logoPath.Substring(logoPath.LastIndexOf(".") + 1);
                logoName = createDir.CreateIconPath(CreateDir.LOGO, gameTypeId, logoType);
                string mapPath = Server.MapPath("/");

                Upload upload = new Upload();
                upload.PostedFile = postFile;
                upload.FileLength = postFile.ContentLength;
                upload.FileName = logoName;
                upload.Extension = logoType;
                upload.SavePath = mapPath;
                upload.UploadStart(logoName);
            }
            return logoName;
        }
    }
}
